package net.sparrow.module;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.GL11;

import net.minecraft.src.Entity;
import net.minecraft.src.EntityAnimal;
import net.minecraft.src.EntityMob;
import net.minecraft.src.EntityPlayer;
import net.minecraft.src.Minecraft;
import net.minecraft.src.RenderManager;
import net.minecraft.src.SpPlayerPostRenderEvent;
import net.sparrow.Sparrow;
import net.sparrow.cmd.CommandHandler;
import net.sparrow.cmd.SpCommand;
import net.sparrow.event.DataField;
import net.sparrow.event.SpEntityPostRenderEvent;
import net.sparrow.event.SpEventListener;
import net.sparrow.event.SpEntityRenderEvent;
import net.sparrow.event.SpEvent;
import net.sparrow.gui.drawing.SpColor;
import net.sparrow.gui.drawing.SpScreenCoord;
import net.sparrow.gui.drawing.font.SpFont;
import net.sparrow.gui.drawing.font.SpFont.SpFontStyle;
import net.sparrow.util.SpLocation;
import net.sparrow.util.Util;

//cookies
public class ESPModule extends Module {

	@DataField( id = "esp.boxes")
	public Boolean boxes = false;

	@DataField( id = "esp.filter")
	public ArrayList<String> espFilter = new ArrayList();

	@DataField( id = "esp.entityxray")
	public Boolean xray = true;
	
	@DataField( id = "esp.seeinvisible")
	public Boolean invisHack = true;

	private List<String> eligibleFilters = Arrays.asList("animals","players","mobs");



	SpFont font = new SpFont(25, new SpColor(1f,1f,1f,.5f), SpFontStyle.NORMAL);
	//GL11 Context: already translated to the entity location
	@SpEventListener
	public void onEntityRender(SpEntityRenderEvent ev) {
		Entity e = ev.entity;
		if( boxes || xray) {

			if (e instanceof EntityMob && espFilter.contains("mobs")) {
				return;
			}

			if (e instanceof EntityAnimal && espFilter.contains("animals")) {
				return;
			}

			if (e instanceof EntityPlayer && espFilter.contains("players")) {
				return;
			}
			//TODO manage invisibility!
			//TODO this only checks modelBipeds
			Sparrow.gui().draw().saveDrawContext();
			Sparrow.gui().draw().disableLightmap();

			GL11.glEnable(GL11.GL_DEPTH_TEST);
			if(boxes) {
				//Sparrow.gui().draw().renderBoundingBox(ev.entity, new SpLocation(0d, 0d, 0d));
			}
			Sparrow.gui().draw().enableLightmap();
			Sparrow.gui().draw().restoreDrawContext();
			if(xray) {
				GL11.glEnable(GL11.GL_POLYGON_OFFSET_FILL);
				GL11.glPolygonOffset(1, -1000000);
				Sparrow.gui().draw().disableLightmap();
			}
			
			if (invisHack && ev.entity.isInvisible()) {
	            GL11.glColor4f(1.0F, 1.0F, 1.0F, 0.15F);
	            GL11.glEnable(GL11.GL_BLEND);
	            GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
	            GL11.glAlphaFunc(GL11.GL_GREATER, 0.003921569F);
			}
		}
	}

	@SpEventListener
	public void onEntityPostRender(SpEntityPostRenderEvent e) {
		if(invisHack && e.entity.isInvisible()) {
            GL11.glDisable(GL11.GL_BLEND);
            GL11.glAlphaFunc(GL11.GL_GREATER, 0.1F);
		}
		if(xray) {
			GL11.glDisable(GL11.GL_POLYGON_OFFSET_FILL);
			GL11.glPolygonOffset(1, 1000000);
			Sparrow.gui().draw().enableLightmap();
		}
	}


	public CommandHandler loadCommands() {
		return new CommandHandler().withDefaultToggle("esp.boxes").withDefaultToggle("esp.entityxray")
				.withDefaultAddRem("esp.filter").withDefaultToggle("esp.seeinvisible");
	}

	
	//todo custom since only animals, players, mobs?
}
